package cn.edu.xjtu.nbrash.xbarsuite.presms;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Base64;

/**
 * PreSMS.java
 *
 * @author Jingkai Tang jingkaitang@gmail.com
 * @version 0.0.1 Nov 21, 2014 12:28:48 AM
 *
 */
public class PreSMS implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1567028722346267800L;

	private int id = NEW_ITEM;
	private String title;
	private String content;
	private static final String BLANK = "";
	private static final int NEW_ITEM = -1;

	public static final String SQL_TABLE_NAME = "PRESMS";
	public static final String SQL_ID = "ID";
	public static final String SQL_TITLE = "TITLE";
	public static final String SQL_CONTENT = "CONTENT";
	public static final String SQL_CREATE_TABLE;

	static {
		StringBuilder sb = new StringBuilder();
		sb.append("CREATE TABLE ").append(SQL_TABLE_NAME).append("(")
				.append(SQL_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ")
				.append(SQL_TITLE).append(" VARCHAR(20), ").append(SQL_CONTENT)
				.append(" VARCHAR(200))");
		SQL_CREATE_TABLE = sb.toString();
	}

	private static final String SQL_SELECT_ALL = "SELECT * FROM "
			+ SQL_TABLE_NAME;
	private static final String SQL_DELETE = "DELETE FROM " + SQL_TABLE_NAME
			+ " WHERE " + SQL_ID + "=?";
	private static final String SQL_UPDATE = "UPDATE " + SQL_TABLE_NAME
			+ " SET " + SQL_TITLE + "=?, " + SQL_CONTENT + "=? WHERE " + SQL_ID
			+ "=?";
	private static final String SQL_INSERT = "INSERT INTO " + SQL_TABLE_NAME
			+ " VALUES(NULL, ?, ?)";

	@SuppressWarnings("unused")
	private PreSMS() {
	}

	/**
	 * Create New One
	 * 
	 * @param title
	 * @param content
	 */
	public PreSMS(String title, String content) {
		setId(NEW_ITEM);
		setTitle(title);
		setContent(content);
	}

	/**
	 * Create Existed ONe
	 * 
	 * @param id
	 * @param title
	 * @param content
	 */
	private PreSMS(int id, String title, String content) {
		this.id = id;
		this.title = title;
		this.content = content;
	}

	public int getId() {
		return id;
	}

	private void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		if (title == null) {
			return BLANK;
		}
		return new String(Base64.decode(title, Base64.DEFAULT));
	}

	public void setTitle(String title) {
		if (title != null) {
			this.title = new String(Base64.encode(title.getBytes(),
					Base64.DEFAULT));
		} else {
			this.title = BLANK;
		}
	}

	public String getContent() {
		if (content == null) {
			return BLANK;
		}
		return new String(Base64.decode(content, Base64.DEFAULT));
	}

	public void setContent(String content) {
		if (content != null) {
			this.content = new String(Base64.encode(content.getBytes(),
					Base64.DEFAULT));
		} else {
			this.content = BLANK;
		}
	}

	public boolean isNewItem() {
		return getId() == NEW_ITEM;
	}

	public static void addPreSMS(Context context, String name, PreSMS preSMS) {
		if (preSMS == null || !preSMS.isNewItem()) {
			return;
		}
		SqlHelper sqlHelper = new SqlHelper(context, name, null, 1);
		SQLiteDatabase db = sqlHelper.getWritableDatabase();
		db.execSQL(SQL_INSERT, new String[] { preSMS.title, preSMS.content });
		sqlHelper.close();
	}

	public static void updatePreSMS(Context context, String name, PreSMS preSMS) {
		if (preSMS == null || preSMS.isNewItem()) {
			return;
		}
		SqlHelper sqlHelper = new SqlHelper(context, name, null, 1);
		SQLiteDatabase db = sqlHelper.getWritableDatabase();
		db.execSQL(SQL_UPDATE, new String[] { preSMS.title, preSMS.content,
				preSMS.id + "" });
		sqlHelper.close();
	}

	public static void deletePreSMS(Context context, String name, PreSMS preSMS) {
		deletePreSMSes(context, name, new PreSMS[] { preSMS });
	}

	public static void deletePreSMSes(Context context, String name,
			PreSMS[] preSMSes) {
		if (preSMSes == null) {
			return;
		}
		SqlHelper sqlHelper = new SqlHelper(context, name, null, 1);
		SQLiteDatabase db = sqlHelper.getWritableDatabase();
		for (PreSMS preSMS : preSMSes) {
			if (preSMS == null || preSMS.isNewItem()) {
				continue;
			}
			db.execSQL(SQL_DELETE, new String[] { preSMS.id + "" });
		}
		sqlHelper.close();
	}

	public static List<PreSMS> getPreSMSes(Context context, String name) {
		SqlHelper sqlHelper = new SqlHelper(context, name, null, 1);
		SQLiteDatabase db = sqlHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(SQL_SELECT_ALL, null);
		int idIndex = cursor.getColumnIndex(SQL_ID);
		int titleIndex = cursor.getColumnIndex(SQL_TITLE);
		int contentIndex = cursor.getColumnIndex(SQL_CONTENT);
		if (idIndex < 0 || titleIndex < 0 || contentIndex < 0) {
			return null;
		}
		List<PreSMS> list = new ArrayList<>();
		while (cursor.moveToNext()) {
			int id = cursor.getInt(idIndex);
			String title = cursor.getString(titleIndex);
			String content = cursor.getString(contentIndex);
			PreSMS preSMS = new PreSMS(id, title, content);
			list.add(preSMS);
		}
		sqlHelper.close();
		return list;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("ID:").append(this.getId()).append(",").append("TITLE:")
				.append(this.getTitle()).append(",").append("CONTENT:")
				.append(this.getContent()).append("\n");
		return sb.toString();
	}

}
